package com.example.suduku;


import java.util.Random;

public class SudokuGenerator {

    private Random rand = new Random();
    // 生成一个简单的数独谜题
    public int[][] generate(int id) {
        int[][] board = new int[9][9];
        fillBoard(board);

       if(id==R.id.btn_easy)
       {
           board=generate_easy( 1 );
       } else if (id==R.id.btn_putong) {
           board = generate_putong( 2 );

       } else if (id==R.id.btn_hard) {
           board = generate_hard( 3 );

       }

        return board;
    }

    // 生成一个简单的数独谜题
    public int[][] generate_easy(int difficulty) {
        int[][] board = new int[9][9];
        fillBoard(board);

        // 删除一定数量的数字来生成谜题
        int removeCount = difficulty == 1 ? 40 : (difficulty == 2 ? 50 : 60);
        removeNumbers(board, removeCount);

        return board;
    }

    // 生成一个普通的数独谜题
    public int[][] generate_putong(int difficulty) {
        int[][] board = new int[9][9];
        fillBoard(board);

        // 删除一定数量的数字来生成谜题
        int removeCount = difficulty == 1 ? 40 : (difficulty == 2 ? 50 : 60);
        removeNumbers(board, removeCount);

        return board;
    }
    // 生成一个困难的数独谜题
    public int[][] generate_hard(int difficulty) {
        int[][] board = new int[9][9];
        fillBoard(board);

        // 删除一定数量的数字来生成谜题
        int removeCount = difficulty == 1 ? 40 : (difficulty == 2 ? 50 : 60);
        removeNumbers(board, removeCount);

        return board;
    }


    // 填充完整的数独
    private void fillBoard(int[][] board) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                board[i][j] = 0;
            }
        }
        solveBoard(board);
    }

    // 解决数独（递归回溯）
    private boolean solveBoard(int[][] board) {
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                if (board[row][col] == 0) {
                    for (int num = 1; num <= 9; num++) {
                        if (isSafe(board, row, col, num)) {
                            board[row][col] = num;
                            if (solveBoard(board)) {
                                return true;
                            }
                            board[row][col] = 0;
                        }
                    }
                    return false;
                }
            }
        }
        return true;
    }

    // 删除数字，生成谜题
    private void removeNumbers(int[][] board, int removeCount) {
        int count = 0;
        while (count < removeCount) {
            int row = rand.nextInt(9);
            int col = rand.nextInt(9);
            if (board[row][col] != 0) {
                board[row][col] = 0;
                count++;
            }
        }
    }

    // 检查数字是否可以放置
    private boolean isSafe(int[][] board, int row, int col, int num) {
        for (int i = 0; i < 9; i++) {
            if (board[row][i] == num || board[i][col] == num) {
                return false;
            }
        }
        int startRow = row / 3 * 3;
        int startCol = col / 3 * 3;
        for (int i = startRow; i < startRow + 3; i++) {
            for (int j = startCol; j < startCol + 3; j++) {
                if (board[i][j] == num) {
                    return false;
                }
            }
        }
        return true;
    }
}
